/**积分订单页面 */
var basePage = require('../../../common/BasePage'); 
var baseOrder = require('../../../common/Order'); 
Page({
    behaviors: [basePage,baseOrder],
    data: {
        active: 0,
        from:'flower',
        orderArr: [{ //订单列表数据
                title: '全部',
                type: 0,
                current_page: 0,
                pages: 1,
                data: [],
                loading: false,
                finished: false,
            },
            
            {
                title: '待发货',
                type: 2,
                loading: false,
                current_page: 0,
                pages: 1,
                finished: false,
                data: [],

            },
            {
                title: '待收货',
                type: 3,
                loading: false,
                current_page: 0,
                pages: 1,
                finished: false,
                data: [],

            },
            
            {
                title: '已完成',
                type: 5,
                loading: false,
                finished: false,
                current_page: 0,
                pages: 1,
                data: [],
            }
        ],
    },
    tab_change(event) {
        console.log("tab_change", event)
        let active = parseInt(event.detail.name);
        this.data.orderArr[active].current_page = 0
        this.setData({
            active
        })
        this.req_orders();
    },
    onLoad: function (options) {
        if (options.active) {
            this.setData({
                active: parseInt(options.active)
            });
        }
        if(options.from){
            this.data.from=options.from;
            
        }
        // this.req_orders();
    },
    onShow: function () {
        this.data.orderArr[this.data.active].current_page = 0;
        this.req_orders();
    },
    login_reload() {
        this.req_orders();
    },
    /**
     * 去评价
     */
    comment_click(e){
        let order=e.currentTarget.dataset.order;

        if(order.order.order_product.length>1){
            wx.navigateTo({
                url: '/pages/cls/comment/Order',
                success: function (obj) {
                    //传递订单数据
                  obj.eventChannel.emit('orderData', order)
                }
              })
        }else{
            wx.navigateTo({
                url: '/pages/cls/myOrder_evaluate/evaluate',
                success: function (obj) {
                  //传递产品数据
                  obj.eventChannel.emit('pdcData',order.order.order_product[0])
                }
              })
        }
       
    },
   
    /**取消订单 */
    cancel_click(e) {
        console.log("cancel_click", e);
        let order = e.currentTarget.dataset.order;
        let index = e.currentTarget.dataset.index;
        let active = this.data.active;
        this.request({
            url: this.data.host + '/home/order/cancel',
            data: {
                order_id: order.order_id,
            },
            method: "POST",
        }, res => {
            // console.log('取消订单成功' , res);
            wx.showToast({
                title: '取消成功',
                icon: 'none'
            })
            order.order_status = -1;
            order.order_status_label = "已取消";
            this.data.orderArr[active].data[index] = order;
            this.setData({
                orderArr: this.data.orderArr
            })
        })
    },
    /**
     * 确认收货
     */
    finished_click(e) {
        console.log("finished_click", e);
        let order = e.currentTarget.dataset.order;
        let index = e.currentTarget.dataset.index;
        let active = this.data.active;
        let that = this;
        wx.showModal({
            title: '提示',
            content: '确认已收到货物？',
            success(res) {
                if (res.confirm) {
                    // console.log('用户点击确定')
                    that.request({
                        url: that.data.host + '/home/order/finished',
                        data: {
                            order_id: order.order_id,
                        },
                        method: "POST",
                    }, res => {
                        if (active != 0) {
                            that.data.orderArr[active].data.splice(index, 1);
                        } else {
                            order.order_status = 8;
                            order.order_status_label = "待评价";
                            that.data.orderArr[active].data[index] = order;
                        }
                        that.setData({
                            orderArr: that.data.orderArr
                        })
                    })
                } 
            }
        })
    },
    /**跳转支付页面 */
    pay_click(e) {
        //  console.log("pay_click",e);
        wx.navigateTo({
             url: '/pages/integral/my_orderPay/pay',
            success: function (res) {
                // 通过eventChannel向被打开页面传送数据
                res.eventChannel.emit('order', e.currentTarget.dataset.order)
            }
        });
    },
    /**
     * 请求订单列表
     */
    req_orders() {
        /**
         * 当前状态的订单数据
         */
        let data = this.data.orderArr[this.data.active];
        let that = this;
        if (data.pages > data.current_page) {
            this.request({
                url: this.data.host + '/home/order/index?type=' + data.type + '&from='+this.data.from,
                data: {
                    page: data.current_page + 1,
                }
            }, res => {
                // console.log("/home/order/index", res);
                data.current_page = res.current_page;
                data.pages = res.pages;
                res.data.map(function (val) {
                    val.order.order_product.map(function (pdc) {
                        pdc.thumb = that.get_thumb(pdc.product_img);
                    });

                })
                data.current_page > 1 ? data.data = data.data.concat(res.data) : data.data = res.data;

                this.setData({
                    orderArr: this.data.orderArr
                })
            })
        }
    },

})